<?php

/* @project : Template
 * @auther  : 青云
 * @date    : 2018/1/10
 * @desc    : 基础模型
 */

namespace app\common\model;

use think\Model;
use think\Db;

class Base extends Model
{
	protected $append = [];

	protected $config = [];

	public function initialize()
	{
		$this->config = config('field.');
	}

	function getIsValidTextAttr($value, $data)
	{

		$is_valid_name = '';

		if (isset($data['is_valid'])) {

			if ($data['is_valid'] == 1) {
				$is_valid_name = '启用';
			}

			if ($data['is_valid'] == 0) {
				$is_valid_name = '禁用';
			}
		}
		return $is_valid_name;
	}

	function get_field()
	{
		$table_name = 'td_' . strtolower(preg_replace('/([a-z])([A-Z])/', "$1" . '_' . "$2", request()->controller()));

		$field = 'COLUMN_NAME,COLUMN_COMMENT';
		$sql   = 'SELECT ' . $field . ' FROM information_schema.COLUMNS WHERE TABLE_NAME = \'' . $table_name . '\'';

		$result = Db::query($sql);
		$arr    = [];
		foreach ($result as $k => $v) {
			$arr[$v['COLUMN_NAME']] = $v['COLUMN_COMMENT'];
		}

		return $arr;
	}

	//获取字段备注
	function getFieldComment()
	{
		$sql = 'show full columns from ' . $this->getTable();

		$ret = $this->query($sql);

		$ret = rotate($ret);

		$list = array_combine($ret['Field'], $ret['Comment']);
		$list = array_merge($list, ['id' => '序号']);
		return $list;
	}

	//表备注
	function getTableComment()
	{
		$sql = "select table_comment from information_schema.TABLES WHERE TABLE_NAME = '" . $this->getTable() . "'";
		$ret = $this->query($sql);

		return $ret[0]['table_comment'];
	}

}
